package com.nf.health.app.intervention_details.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.intervention_details.activity.HealthSurveyActivity;
import com.nf.health.app.intervention_details.bean.DoctorOrderDetailsBean;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.TextUtilTools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 恩福 on 2016/11/24.
 * 医嘱详情里面的干预调查问卷 Adapter
 *
 * @auther changpeng
 * @email obj_cp@126.com
 */

public class DoctorOrderQuestionAdapter extends RecyclerView.Adapter {

    private List<DoctorOrderDetailsBean.QuestionListBean> questionList;
    private Context mContext;

    public DoctorOrderQuestionAdapter(Context context, List<DoctorOrderDetailsBean.QuestionListBean> questionList) {
        this.mContext = context;
        this.questionList = questionList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.doctor_order_questionnaire, parent, false);
        return new DoctorOrderQuestionHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final DoctorOrderDetailsBean.QuestionListBean questionListBean = questionList.get(position);
        DoctorOrderQuestionHolder doctorOrderQuestionHolder = (DoctorOrderQuestionHolder) holder;
        String questionnaire = "<font color='#2E86F4'>"+questionListBean.getSurveytime()+"</font>&nbsp;&nbsp;&nbsp; " +
                "第"+(position+1)+"次干预调查问卷 ";

        TextUtilTools.setTextFromHtml(doctorOrderQuestionHolder.tvQuestionnaire,questionnaire);
        doctorOrderQuestionHolder.tvFillQuestionnaire.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(HealthSurveyActivity.EXTRA_MSG_ID, questionListBean.getId());
                ActivityUtils.showActivity(mContext, HealthSurveyActivity.class, bundle);
            }
        });
        String quesTime = questionListBean.getSurveytime();

        SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String time = formatter.format(curDate);
        try {
            Date quesTimeDate = formatter.parse(quesTime);
            long quesTimeLong = quesTimeDate.getTime()/1000;

            Date curTimeDate = formatter.parse(time);
            long curTimeLong = curTimeDate.getTime()/1000;

            if(quesTimeLong <= curTimeLong){
                doctorOrderQuestionHolder.tvFillQuestionnaire.setEnabled(true);
                doctorOrderQuestionHolder.tvFillQuestionnaire.setTextColor(mContext.getResources().getColor(R.color.theme_color));
            }else{
                //还没到时间，不让填写
                doctorOrderQuestionHolder.tvFillQuestionnaire.setEnabled(false);
                doctorOrderQuestionHolder.tvFillQuestionnaire.setTextColor(mContext.getResources().getColor(R.color.black8));
            }

            if(questionListBean.getState().equals("1")){
                doctorOrderQuestionHolder.tvFillQuestionnaire.setEnabled(false);
                doctorOrderQuestionHolder.tvFillQuestionnaire.setBackgroundResource(0);
                doctorOrderQuestionHolder.tvFillQuestionnaire.setTextColor(mContext.getResources().getColor(R.color.theme_color));
                doctorOrderQuestionHolder.tvFillQuestionnaire.setText("已完成");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return questionList == null ? 0 : questionList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    class DoctorOrderQuestionHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_questionnaire)
        TextView tvQuestionnaire;
        @Bind(R.id.tv_fill_questionnaire)
        TextView tvFillQuestionnaire;

        public DoctorOrderQuestionHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
